const log = require('../util/log'),
    config = require('../util/config'),
    logger = require('log4js').getLogger(),
    taobaoClient = require('../mng/taobaoClientMng').client

module.exports = (req, res) => {
    require('../util/service').handlePost(req, res, async (reqObj, resolve) => {
        reqObj.q = reqObj.q || ''
        taobaoClient.execute('taobao.tbk.dg.item.coupon.get', {
            adzone_id: '57204670',
            q: reqObj.q
        }, (err, result) => {
            if (err) {
                logger.error('[' + __filename + ':' + __line + ']', err);
                return
            }
            var list = []
            if (!result.results) {
                logger.error('[' + __filename + ':' + __line + ']', result);
            } else {
                result.results.tbk_coupon.forEach(item => {
                    list.push({
                        id: item.num_iid,
                        img: item.pict_url,
                        title: item.title,
                        info: item.coupon_info,
                        count: item.coupon_remain_count,
                        shopName: '',
                        link: item.coupon_click_url
                    })
                })
            }
            resolve({
                list: list
            })
        })
    })
}
